# -*- coding: utf-8 -*-
# @Date    : 2014-08-29 14:23:30
# @Author  : Your Name (you@example.org)
# @Link    : http://example.org
# @Version : $Id$
'''

    1.将字符串的时间转换为时间戳
        方法:
            a = "2013-10-10 23:40:00"
            将其转换为时间数组
            import time
            timeArray = time.strptime(a, "%Y-%m-%d %H:%M:%S")
        转换为时间戳:
        timeStamp = int(time.mktime(timeArray))
        timeStamp == 1381419600

    2.字符串格式更改
        如a = "2013-10-10 23:40:00",想改为 a = "2013/10/10 23:40:00"
        方法:先转换为时间数组,然后转换为其他格式
        timeArray = time.strptime(a, "%Y-%m-%d %H:%M:%S")
        otherStyleTime = time.strftime("%Y/%m/%d %H:%M:%S", timeArray)


    3.时间戳转换为指定格式日期:
        方法一:
            利用localtime()转换为时间数组,然后格式化为需要的格式,如
            timeStamp = 1381419600
            timeArray = time.localtime(timeStamp)
            otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)
            otherStyletime == "2013-10-10 23:40:00"

        方法二:
            import datetime
            timeStamp = 1381419600
            dateArray = datetime.datetime.utcfromtimestamp(timeStamp)
            otherStyleTime = dateArray.strftime("%Y-%m-%d %H:%M:%S")
            otherStyletime == "2013-10-10 23:40:00"

    4.获取当前时间并转换为指定日期格式
        方法一:
            import time
            获得当前时间时间戳
            now = int(time.time())  ->这是时间戳
            转换为其他日期格式,如:"%Y-%m-%d %H:%M:%S"
            timeArray = time.localtime(timeStamp)
            otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)

        方法二:
            import datetime
            获得当前时间
            now = datetime.datetime.now()  ->这是时间数组格式
            转换为指定的格式:
            otherStyleTime = now.strftime("%Y-%m-%d %H:%M:%S")

    5.获得三天前的时间
        方法:
            import time
            import datetime
            先获得时间数组格式的日期
            threeDayAgo = (datetime.datetime.now() - datetime.timedelta(days = 3))
            转换为时间戳:
                timeStamp = int(time.mktime(threeDayAgo.timetuple()))
            转换为其他字符串格式:
                otherStyleTime = threeDayAgo.strftime("%Y-%m-%d %H:%M:%S")
        注:timedelta()的参数有:days,hours,seconds,microseconds

    6.给定时间戳,计算该时间的几天前时间:
        timeStamp = 1381419600
        先转换为datetime
        import datetime
        import time
        dateArray = datetime.datetime.utcfromtimestamp(timeStamp)
        threeDayAgo = dateArray - datetime.timedelta(days = 3)
        参考5,可以转换为其他的任意格式了

'''
import time
import datetime

def get_now_timestamp():
    return int(time.time() * 1000)

def get_now():
    return int(time.time())

def get_local_datetime(split='-'):
    dt = ['%Y', split, '%m', split, '%d', ' %H:%M:%S']
    datestr = ''.join(dt)
    return time.strftime(datestr, time.localtime())

def get_local_date(split='-'):
    dt = ['%Y', split, '%m', split, '%d']
    datestr = ''.join(dt)
    return time.strftime(datestr, time.localtime())

def str_to_timestamp(strtime, split='-', yearlen=4, hassec=True):
    strarr = ['%Y', split, '%m', split, '%d', ' %H:%M']
    if yearlen == 2:
        strarr[0] = '%y'
    if hassec == True:
        strarr.append(':%S')
    timestr = ''.join(strarr)
    try:
        timeArray = time.strptime(str(strtime), timestr)
    except Exception, e:
        return -1
    
    return int(time.mktime(timeArray))

def get_today():
    today = datetime.datetime.now()
    return today

def get_timestamp(hour=0, mins=0, sec=0):
    today = get_today()
    some_clock = datetime.datetime(today.year, today.month, today.day, hour, mins, sec)
    timeStamp = int(time.mktime(some_clock.timetuple()))
    return timeStamp

def get_start_timestamp(dt):
    '''传入参数格式：2015-1-12'''
    dt = dt + ' 00:00:00'
    start_str = "%Y-%m-%d %H:%M:%S"
    return _timestamp(dt, start_str)

def get_end_timestamp(dt):
    '''传入参数格式：2015-1-12'''
    dt = dt + ' 23:59:59'
    end_str = "%Y-%m-%d %H:%M:%S"
    return _timestamp(dt, end_str)

def _timestamp(dt, time_str):
    '''格式化字符串日期，转换为int型timestamp'''
    timeArray = time.strptime(dt, time_str)
    timeStamp = int(time.mktime(timeArray))
    return timeStamp

def timestamp_2_date(timeStamp):
    '''timestamp转换为制定格式日期字符串'''
    # dateArray = datetime.datetime.utcfromtimestamp(timeStamp)
    dateArray = datetime.datetime.fromtimestamp(timeStamp)
    date_time = dateArray.strftime("%Y-%m-%d")
    return date_time

def timestamp_2_datetime(timeStamp, ftime="%Y-%m-%d %H:%M:%S"):
    '''timestamp转换为制定格式日期字符串'''
    # dateArray = datetime.datetime.utcfromtimestamp(timeStamp)
    dateArray = datetime.datetime.fromtimestamp(timeStamp)
    date_time = dateArray.strftime(ftime)
    return date_time

def time_2_str(time_str):
    '''时间转换数字，如：11:11:22 转换后为011122'''
    a = time_str.split(':')
    b = ''
    for i in a:
        b = b + i
    return b

def str_2_time(time_str):
    '''数字转换到时间， 如111122转换后11：11：22'''
    b = '%s:%s:%s' % (time_str[:2],time_str[2:4],time_str[4:6])
    return b

def today_timestamp():
    '''今天开始stamp'''
    d = get_today()
    oneday = datetime.timedelta(days=1)
    day = d - oneday
    date_from = datetime.datetime(day.year, day.month, day.day, 0, 0, 0)
    date_to = datetime.datetime(day.year, day.month, day.day, 23, 59, 59)
    print '---'.join([str(date_from), str(date_to)])
 
def week_ago_timestamp():
    '''上一周开始stamp'''
    d = get_today()
    dayscount = datetime.timedelta(days=d.isoweekday())
    dayto = d - dayscount
    sixdays = datetime.timedelta(days=6)
    dayfrom = dayto - sixdays
    date_from = datetime.datetime(dayfrom.year, dayfrom.month, dayfrom.day, 0, 0, 0)
    date_to = datetime.datetime(dayto.year, dayto.month, dayto.day, 23, 59, 59)
    print '---'.join([str(date_from), str(date_to)])
 
def month_ago_timestamp():
    '''一月开始stamp'''
    d = get_today()
    dayscount = datetime.timedelta(days=d.day)
    dayto = d - dayscount
    date_from = datetime.datetime(dayto.year, dayto.month, 1, 0, 0, 0)
    date_to = datetime.datetime(dayto.year, dayto.month, dayto.day, 23, 59, 59)
    print '---'.join([str(date_from), str(date_to)])

def get_day_start(day_time=0):
    d = get_today()
    oneday = datetime.timedelta(days=day_time)
    day = d - oneday
    date_from = datetime.datetime(day.year, day.month, day.day, 0, 0, 0)
    
    return _timestamp(str(date_from), '%Y-%m-%d %H:%M:%S')


def timeit(func):
    def run(*argv):
        print func.__name__
        if argv:
            ret = func(*argv)
        else:
            ret = func()
        return ret
    return run
@timeit
def t(a):
    print a
# t(1)

